package com.visceb.backstage.controller;
import com.visceb.backstage.dao.subjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.visceb.backstage.entity.Subject;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("subject")
@CrossOrigin()
public class subjectController {
    @Autowired
    private subjectRepository subjectrepository;

    //课程概览信息初始化接口
    @RequestMapping(value ="/init", method = RequestMethod.GET)
    @ResponseBody
    public List<Subject> subjectInit() {
        List<Subject> subjectAll = subjectrepository.findAll();
//        System.out.print(subjectAll);
        return subjectAll;
    }

    @RequestMapping(value ="/deleteSub", method = RequestMethod.POST)
    @Transactional
    public int deleteSub(@RequestBody int subId) {
        int subjectAll = subjectrepository.deleteById(subId);
//        System.out.print(subjectAll);
        return subjectAll;
    }

//    课程信息筛选框接口，能够根据年级和学院选择
    @RequestMapping(value ="/select", method = RequestMethod.POST)
    @ResponseBody
    public List<Subject> subjectSelect(@RequestBody Map<String, String> subject) {
        List<Subject> selectSubject;
        if(subject.get("academy").isEmpty() || subject.get("grade").isEmpty()){
            selectSubject = subjectrepository.findAllByAcademyOrGrade(subject.get("academy"), subject.get("grade"));
        }else{
            selectSubject = subjectrepository.findAllByAcademyAndGrade(subject.get("academy"), subject.get("grade"));
        }
        return selectSubject;
    }

//    搜索功能接口，可模糊查询课程名
//    此处也可以设置为可查询课程名，老师名，等。只需要在dao层设置findAllBy...Or...Or...()函数
    @RequestMapping(value ="/find", method = RequestMethod.GET)
    @ResponseBody
    public List<Subject> subjectFind(String subjectName) {
//        System.out.println(subjectName);
        List<Subject> findSubject;
        findSubject = subjectrepository.findAllBySubjectNameLike("%"+ subjectName +"%");
        return findSubject;
    }

//    点击查看详情后找到对应id的信息
    @RequestMapping(value ="/findId", method = RequestMethod.GET)
    @ResponseBody
    public Subject subjectFindId(String id) {
        Subject findSubject;
        findSubject = subjectrepository.findById(Integer.parseInt(id));
        return findSubject;
    }
}
